package org.example.Swing.handler;

import org.example.Dao.GradeDao;
import org.example.Entity.Student;
import org.example.Main;
import org.example.Swing.xiangmu.login.AddView;
import org.example.Swing.xiangmu.login.LoginView;
import org.example.Swing.xiangmu.login.MainView;
import org.example.Swing.xiangmu.login.UpdateView;
import org.example.Utils.MybatisUtils;
import org.example.controller.GradeController;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class MainHandler implements ActionListener {
    private MainView mainView;
    public MainHandler(MainView mainView) {
        this.mainView = mainView;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton jButton = (JButton) e.getSource();
        String text = jButton.getText();
        if ("增加".equals(text)) {
            new AddView(mainView);
        } else if ("修改".equals(text)) {

            String selectedStudentIds = mainView.getSelectedStudentIds();
            int crouse=0;
            if (mainView.getSelectedScoreIds() != null) {
                int crouseId = Integer.parseInt(mainView.getSelectedScoreIds());
                crouse=crouseId;
            }
            if (selectedStudentIds != null&&crouse!=0) {
                int option=JOptionPane.showConfirmDialog(mainView, "是否修改该成绩!","确认修改",JOptionPane.YES_NO_OPTION);
                if (option==JOptionPane.YES_OPTION) {
                    mainView.dispose();
                    new UpdateView(mainView, selectedStudentIds,crouse);
                }
            }else{
                JOptionPane.showMessageDialog(mainView, "请选择需要修改的成绩！！!");
            }

        } else if ("删除".equals(text)) {
            String selectedScoreId= mainView.getSelectedScoreIds();
            String selectedStudentIds = mainView.getSelectedStudentIds();
            if (selectedStudentIds == null) {
                JOptionPane.showMessageDialog(mainView, "请选择要删除的行!");
                return;
            }
            int option = JOptionPane.showConfirmDialog(mainView, "你确认要删除该学生的成绩吗?", "确认删除",
                    JOptionPane.YES_NO_OPTION);
            if (option == JOptionPane.YES_OPTION) {
                GradeController gradeController=new GradeController();
                List<Student> b=gradeController.selectByGradeOne(selectedStudentIds);
                if (b.size()>1) {
                    int i = gradeController.deleteByStudent(selectedStudentIds, Integer.valueOf(selectedScoreId));
                    System.out.println(i);
                    if (i > 0) {
                        mainView.dispose();
                        JOptionPane.showMessageDialog(mainView, "删除成功!");
                        new MainView();
                    }else {
                        JOptionPane.showMessageDialog(mainView, "删除失败!");
                    }
                }else if (b.size()==1){
                    GradeDao mapper = MybatisUtils.getMapper(GradeDao.class);
                    int c = mapper.deleteGrade(Integer.parseInt(selectedScoreId), selectedStudentIds);
                    int i= mapper.deleteStudent(selectedStudentIds);
                    //提交事务
                    MybatisUtils.commit();
                    if (i > 0 && c > 0) {
                        JOptionPane.showMessageDialog(mainView, "删除成功!");
                        new MainView();
                        mainView.dispose();
                    }else {
                        JOptionPane.showMessageDialog(mainView, "删除失败!");
                        new MainView();
                        mainView.dispose();
                    }
                }else {
                    JOptionPane.showMessageDialog(mainView, "该学生最后的成绩!");
                }

            }
        } else if ("查询".equals(text)) {
        } else if ("上一页".equals(text)) {
            mainView.dispose();
            new MainView();
        } else if ("下一页".equals(text)) {
            mainView.dispose();
            new MainView();
        }

    }

    public void fanhui() {
        mainView.dispose();
        new LoginView();
    }
}